<?php defined('SYSPATH') or die('No direct script access.');
/*
 * 运营管理促销信息控制器
 */
class Controller_Admin_Cuxiao extends Controller_Template {

	public function __construct ($req, $res)
	{
		parent::__construct ($req, $res);
		$this->template = 'smarty:admin/cuxiao/list';
	}
    /**
	 * 默认入口
	 *
	 */
	public function action_index()
	{
	    $user_cities = User::cities();
		if($user_cities)
		{
		  $current_city = current($user_cities);
          $current_city_id = $current_city['id'];
		}
		$url = '/cuxiao/list/all/'.$current_city_id .'/1';
		$this->request->redirect($url);
	}

    /**
	 * 促销信息列表
	 * @int $city_id  城市id
	 * @int $page     当前页码
	 * @string $type  查询条件
	 *
	 */
	public function action_list($type='all',$city_id,$page=1)
	{
		// 搜索的类型判定
	    $status_type = kohana::config('cuxiao.status_name');
		if($type == 'all')
		{
		   $status_search_type  = array();
		}
		else
		{
            if(!$status_type[$type])
			{
			  throw new SCException("类别错误");
			}
		   $status_search_type = array($type);
		   
		   // 20120926促销新壮态修改
		   $type == 'breakrules' && $status_search_type[] = 'breakconfirm';
		}
	    $user_cities = User::cities();

		 foreach($user_cities as $value)
		 {
		    if($city_id == $value['id'])
			{
			    $city_find = true;
				break;
			}
		 }

		 if(!$city_find)
		 {
			throw new SCException("参数错误");
		 }

	     $city_id  =intval($city_id);
         if($city_id)
		   {
		       $query = array('city'=> $city_id, 
					   'status'     => $status_search_type,
					   'order_by'   => array('ctime'=>'DESC'),
					   );
			   if($this->request->post())
			   {
			      $search_title = $this->request->post('title');
				  $query['title']=trim($search_title);
				  $page  = 1;
			   }
			   $limit = kohana::config('shop.cxlist.limit');
			   $start = $limit*($page-1);
			   if($start <= 0)
			   {
				   $start = 0;
			   }
			   
			   $cuxiao_list    = CuXiao::Search($query,$start,$limit);
			   $cuxiao_count   = intval($cuxiao_list['count']);
			   $page_total     = ceil($cuxiao_count/$limit);
			   $page_config    = array(
				       'current_page'     => array('source'=> 'route','key'=>'param3'),
					   'total_items'      => $cuxiao_count,
					   'items_per_page'   => $limit,
					// 'view'             => 'pagination/basic',
					   'auto_hide'        => true,
					   'first_page_in_url'=> true,
			    );

			    $cuxiao_lists = array();
			    if($cuxiao_list['list']){
				   foreach($cuxiao_list['list'] as $key => $val)
				   {
				     $cuxiao_lists[$key]['shop_name'] = Shop::name($val['shop_id']);
					 $cuxiao_lists[$key]['shop_url']  = Shop::shop_detail_url($val['shop_id']);
					 $cuxiao_lists[$key]['title']     = $val['title'];
					 $cuxiao_lists[$key]['creat_time']= $val['create_time'];
					 $cuxiao_lists[$key]['end_time']  = $val['end_time'];
					 $cuxiao_lists[$key]['id']        = $key;
					 $cuxiao_lists[$key]['url']       = CuXiao::detail_url($key);  
					 $cuxiao_lists[$key]['status']    = $val['status'];
			         if(($type == 'breakrules')||($type == 'removed'))
					 {
					    $message= Cuxiao::get_message($key,$type);
						$cuxiao_lists[$key]['uid']    = $message[$key]['uid']; 
						$cuxiao_lists[$key]['reason'] = $message[$key]['reason']; 
						$cuxiao_lists[$key]['msg']    = $message[$key]['msg']; 
					 }
				   }
				}
				//$arr_result = Service_Jubao::search(array());
				//print_r($arr_result);
				$pagination = Pagination::factory($page_config);
		        $current_time = time();
				$msg = kohana::config('cuxiao.msg');
				$this->template->set('msg',$msg);
				$this->template->set('pagination',$pagination->render());
				$this->template->set('cuxiao_lists',$cuxiao_lists);
				$this->template->set('city_id',$city_id);
				$this->template->set('count',$cuxiao_count);
				$this->template->set('current_time',$current_time);
				$this->template->set('type',$type);
				$this->template->set('user_cities',$user_cities);

		   }
	}

	/**
	 * 处理指定的促销信息
	 * @int $cuxiao_id 促销信息id
	 * @int $city_id   城市id
	 *
	 */
	public function action_set($type,$id,$city_id)
	{
	   $city_id   = intval($city_id);
	   // 对用户的操作城市进行判定
	   $cities    = user::cities();
       if($cities)
	   {
	       if($cities[$city_id])
		   {
	        $status    = kohana::config('cuxiao.status_name');
	        if(!$status[$type])
	        {
	           throw new SCException('类别错误');
			}

			if($this->request->post())
			{
		        $reason    = (int)$this->request->post('reason');
		        $cuxiao_id = (int)$this->request->post('cuxiao');
		        $msg       = (string)$this->request->post("content_jb");
	
		        if(($type == 'removed')||($type = 'breakrules'))
		        {
			      $explain = array(
			        'reason'=> $reason,
				    'msg'   => $msg,
				    'uid'   => user::current_user_id(),
			      );
		          $status  = Cuxiao::set_status($cuxiao_id,$type,$explain);
		        }
			}
			else
			{ 
			  if($type == 'valid')
			  {
				   $cuxiao_id  = (int)$id; 	
		           $status     = Cuxiao::set_status($cuxiao_id,$type);
			  }
			}
	       }
       }

	   // 返回页面
	   $url       = $_SERVER["HTTP_REFERER"];
	   if(!$url)
	   {
	      $url = '/cuxiao/list/all/'.$city_id;
	   }
		$this->request->redirect($url);
    }
}
